package system.listener;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.*;

@Component
public class PermissionListener {

    public static List<Map<String, String>> mapList;

    public PermissionListener(@Qualifier("requestMappingHandlerMapping") RequestMappingHandlerMapping mapping) {
        List<Map<String, String>> requests = new ArrayList<>();
        Map<RequestMappingInfo, HandlerMethod> tmp = mapping.getHandlerMethods();
        Iterator<Map.Entry<RequestMappingInfo, HandlerMethod>> it = tmp.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<RequestMappingInfo, HandlerMethod> entry = it.next();
            RequestMappingInfo key = entry.getKey();
            HandlerMethod handlerMethod = entry.getValue();
            Map<String, String> req = new HashMap<>();
            req.put("name", handlerMethod.getBeanType().getSimpleName() + "_" + handlerMethod.getMethod().getName());
            req.put("url", key.getPatternsCondition().toString().replaceAll("\\[|\\]", ""));
            requests.add(req);
        }
        mapList = requests;
    }
}
